import random as rand
import math
from datetime import date, timedelta
import docx
from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT

oper = ['+', '-', "*", "/"]
colName = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
doc = docx.Document()


def saveToDocx(dataList: list, colNum: int = 3, timeDelta: int = 0):
    dataLen = len(dataList)
    rowNum = math.ceil(dataLen/colNum)
    idx = 0
    lineNum = '\n' * 3
    if timeDelta == 0:
        lineNum = ''
    ph = doc.add_paragraph(lineNum + '序号：' + str(timeDelta) + '          日期：______________' +  '          分数_____')
    font = ph.runs[0].font
    font.size = docx.shared.Pt(16)
    table = doc.add_table(rows=rowNum, cols=colNum)
    for i in range(0, rowNum):
        for j in range(0, colNum):
            cellTmp = table.cell(i, j)

            # p = cellTmp.add_paragraph('')
            cellTmp.text = dataList[idx]

            cellTmp.paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
            cellTmp.vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER
            cellTmp.vertical_alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
            cellTmp.paragraphs[0].runs[0].font.bold = True
            font = cellTmp.paragraphs[0].runs[0].font
            font.size = docx.shared.Pt(16)
            idx += 1
            if idx == dataLen:
                break


''' 小学生加减乘除基础运算 '''
if __name__ == '__main__':

    for p in range(1, 11):
        express = []
        for i in range(1, 31):
            opCode = oper[rand.randint(0, 2)]
            if opCode == '*':
                num1 = rand.randint(2, 9)
                numMid = rand.randint(2, 9)
                num2 = num1 * numMid
            else:
                num1 = rand.randint(1, 100)
                num2 = rand.randint(1, 100)

            if opCode == '+':
                if num1 >= num2:
                    express.append(str(num2) + ' + (     )' +
                                   ' = ' + str(num1))
                else:
                    express.append(str(num1) + ' + (     )' +
                                   ' = ' + str(num2))
            elif opCode == '-':
                if num1 >= num2:
                    express.append(str(num1) + ' - (     )' +
                                   ' = ' + str(num2))
                else:
                    express.append(str(num2) + ' - (     )' +
                                   ' = ' + str(num1))
            else:
                express.append(str(num1) + ' * (     )' + ' = ' + str(num2))
        saveToDocx(express, 3, p)
    doc.save('数学题.docx')
